//
//  MyViewController.m
//  Romens.XSBalance
//
//  Created by 王腾 on 16/2/23.
//  Copyright © 2016年 孙博. All rights reserved.
//

#import "MyViewController.h"
#import "ToolHelper.h"
#import "listTableViewCell.h"
#import "subListTableViewCell.h"
#import "UserInfoEditViewController.h"
#import "userInformation.h"
#import "UserInfoEditViewController.h"
#import "DeviceSearchController.h"
#import "RuntimeStatus.h"
#import "MJExtension.h"
#import "MeasureViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "PwdViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITabBarDelegate,UIAlertViewDelegate>
{
    
}
@property (nonatomic,strong)UITableView * listTableView;
@property (strong, nonatomic) NSMutableArray *isShowSubReport;
@property (strong, nonatomic) NSMutableArray *memberArray;
@property (strong, nonatomic) NSMutableArray *deviceArray;
@property(nonatomic,strong)UIButton * quitButton;//退出登录；
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
   // self.title=@" ";
    self->_isShowSubReport=[[NSMutableArray alloc]init];
    if (self->_isShowSubReport.count==0)
    {
        for (int i=0; i<2; i++)
        {
            [self->_isShowSubReport addObject:[NSNumber numberWithBool:NO]];
        }
    }
    [self initDeviceAndMemberArray];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addUserNotification:) name:@"UserChange" object:nil];
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)initDeviceAndMemberArray
{
    RuntimeStatus * run=[RuntimeStatus instance];
    _memberArray=[NSMutableArray arrayWithArray:run.ALLUsers];
    
    NSUserDefaults * defaults=[NSUserDefaults standardUserDefaults];
    NSString * jsonString=[defaults objectForKey:@"DeviceAlls"];
    if (jsonString==nil) {
        _deviceArray=[[NSMutableArray alloc]init];
    }
    else
    {
        id array=[ToolHelper JsonToDictionary:jsonString];
        _deviceArray=[NSMutableArray arrayWithArray:array];
    }

}
-(void)addUserNotification:(NSNotification*)notift
{
    [self initDeviceAndMemberArray];
    [self.listTableView reloadData];
}
-(void)initView{
    self.listTableView=[[UITableView alloc]init];
    self.listTableView.translatesAutoresizingMaskIntoConstraints=NO;
    self.listTableView.delegate=self;
    self.listTableView.dataSource=self;
    self.listTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.listTableView];
    
    
    self.quitButton=[[UIButton alloc]init];
    self.quitButton.translatesAutoresizingMaskIntoConstraints=NO;
    self.quitButton.backgroundColor=SystemColor;
    [self.quitButton setTitle:@"退出" forState:UIControlStateNormal];
    self.quitButton.layer.cornerRadius = 5.0f;//圆角半径
    [self.quitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.quitButton addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.quitButton];
    
    
    [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_listTableView) format:@"H:|[_listTableView]|"]];
    [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_quitButton) format:@"H:|-[_quitButton]-|"]];
    [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_listTableView,_quitButton) format:@"V:|[_listTableView]-[_quitButton(40)]-8-|"]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
            //修改密码；
        case 2:
            return 1;
            break;
            
        default:
        {
            
            if (![[self->_isShowSubReport objectAtIndex:section] boolValue]) {
                return 1;
            }
            
            else
            {
                switch (section)
                {
                    case 0:
                        return _memberArray.count+2;
                        break;
                    case 1:
                        return _deviceArray.count+2;
                        break;
                }
                return 0;
            }
        }
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   listTableViewCell* listCell=[tableView dequeueReusableCellWithIdentifier:@"listCell"];
    if (listCell==nil) {
        listCell=[[listTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"listCell"];
    }
   subListTableViewCell * subListCell=[tableView dequeueReusableCellWithIdentifier:@"subListCell"];
    if (subListCell==nil) {
        subListCell=[[subListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"subListCell"];
    }
    switch (indexPath.section)
    {
        case 0:
        {
            if  (![[self->_isShowSubReport objectAtIndex:indexPath.section] boolValue])
            {
                listCell.listNameLabel.text=NSLocalizedString(@"家庭成员",nil);
                listCell.listImage.image=[UIImage imageNamed:@"家庭成员"];
                listCell.arrowImage.image=[UIImage imageNamed:@"right"];
                return listCell;
            }
            else
            {
                
                if (indexPath.row==0) {
                    listCell.listNameLabel.text=NSLocalizedString(@"家庭成员",nil);
                    listCell.listImage.image=[UIImage imageNamed:@"家庭成员"];
                    listCell.arrowImage.image=[UIImage imageNamed:@"down"];
                    return listCell;
                }
                else if (indexPath.row==_memberArray.count+1)
                {
                    subListCell.subNameLabel.text=NSLocalizedString(@"添加成员",nil);
                    subListCell.subImage.image=[UIImage imageNamed:@"添加"];
                    subListCell.subArrowImage.hidden=YES;
                    return subListCell;
                }
                else
                {
                    userInformation * user=[_memberArray objectAtIndex:indexPath.row-1];
//                    NSDictionary * dic=[_memberArray objectAtIndex:indexPath.row-1];
                    subListCell.subNameLabel.text=user.USERNAME;
                    subListCell.subImage.image=[UIImage imageNamed:@"身份"];
                    subListCell.subArrowImage.hidden=NO;
                    subListCell.subArrowImage.image=[UIImage imageNamed:@"right"];
                    return subListCell;
                }
                
            }
            
        }
            break;
            
        case 1:
        {
            if  (![[self->_isShowSubReport objectAtIndex:indexPath.section] boolValue]) {
                listCell.listNameLabel.text=NSLocalizedString(@"我的设备",nil);
                listCell.listImage.image=[UIImage imageNamed:@"我的设备"];
                listCell.arrowImage.image=[UIImage imageNamed:@"right"];
                return listCell;
            }
            else
            {
                
                if (indexPath.row==0) {
                    listCell.listNameLabel.text=NSLocalizedString(@"我的设备",nil);
                    listCell.listImage.image=[UIImage imageNamed:@"我的设备"];
                    listCell.arrowImage.image=[UIImage imageNamed:@"down"];
                    return listCell;
                }
                else if (indexPath.row==_deviceArray.count+1)
                {
                    subListCell.subNameLabel.text=NSLocalizedString(@"添加设备",nil);
                    subListCell.subImage.image=[UIImage imageNamed:@"添加"];
                    subListCell.subArrowImage.hidden=YES;
                    return subListCell;
                }
                else
                {
                    NSDictionary *dic=[_deviceArray objectAtIndex:indexPath.row-1];
                    subListCell.subNameLabel.text=[dic objectForKey:@"name"];
                    subListCell.subImage.image=[UIImage imageNamed:@"蓝牙脂肪秤"];
                    subListCell.subArrowImage.hidden=NO;
                    subListCell.subArrowImage.image=[UIImage imageNamed:@"right"];
                    return subListCell;
                }
            }
        }
            break;
            case 2:
        {
            listCell.listNameLabel.text=@"修改密码";
            listCell.listImage.image=[UIImage imageNamed:@"修改密码"];
            listCell.arrowImage.image=[UIImage imageNamed:@"right"];
            return listCell;
        }
            break;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        switch (indexPath.section)
        {
            case 2:
            {

                PwdViewController * pwd=[[PwdViewController alloc]init];
                pwd.hidesBottomBarWhenPushed=YES;
                [self.navigationController pushViewController:pwd animated:YES];
            }
                break;
                
            default:
            {
            if([[self->_isShowSubReport objectAtIndex:indexPath.section]boolValue])
            {
                [self->_isShowSubReport setObject:[NSNumber numberWithBool:NO] atIndexedSubscript:indexPath.section];
            }else{
                [self->_isShowSubReport setObject:[NSNumber numberWithBool:YES] atIndexedSubscript:indexPath.section];
            }
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            }
                break;
        }
    }
    else
    {
        switch (indexPath.section) {
            case 0:
            {
                if (indexPath.row==_memberArray.count+1) {
                    
                    UserInfoEditViewController * userVC=[[UserInfoEditViewController alloc]init];
                    userVC.ISONESELF=@"1";
                    userVC.IsEdit=@"0";
                    userVC.hidesBottomBarWhenPushed=YES;
                    __weak UserInfoEditViewController * _userVCon=userVC;
                    userVC.backBlock=^(){
                        [_userVCon.navigationController popViewControllerAnimated:YES];
                    };
                    [self.navigationController pushViewController:userVC animated:YES];
                }
                else
                {
                    userInformation * user=[_memberArray objectAtIndex:indexPath.row-1];
                    userInformation *userCopy=[user mutableCopy];
                    
                    UserInfoEditViewController * userVC=[[UserInfoEditViewController alloc]init];
                    userVC.ISONESELF=userCopy.ISONESELF;
                    userVC.IsEdit=@"1";
                    userVC.hidesBottomBarWhenPushed=YES;
                    __weak UserInfoEditViewController * _userVCon=userVC;
                    userVC.backBlock=^(){
                        [_userVCon.navigationController popViewControllerAnimated:YES];
                    };
                    _userVCon.userinfor=userCopy;
                    [self.navigationController pushViewController:userVC animated:YES];

                }
            }
                break;
                
            case 1:
            {
                if (indexPath.row==_deviceArray.count+1)
                {
                    DeviceSearchController * deviceVC=[[DeviceSearchController alloc]init];
                   deviceVC.hidesBottomBarWhenPushed=YES;
                   [self.navigationController pushViewController:deviceVC animated:YES];
                }
                else
                {

                    NSDictionary *dic=[_deviceArray objectAtIndex:indexPath.row-1];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"addDevices" object:dic];
                    [self.tabBarController setSelectedIndex:0];
                }
            }
                break;
        }
    }
    
    
}
# pragma mark-退出登录；
-(void)quit{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确定退出？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil] ;
   
    [alert addButtonWithTitle:@"确定"];
    [alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self ClearLogin];
        [self pushToLoginView];
           }
}
-(void)pushToLoginView
{
   __weak AppDelegate * app=[UIApplication sharedApplication].delegate;
   
    [app Login];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
